package com.pudu.mydemo.act;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.ImageView;

import com.pudu.pdrobot.App;
import com.pudutech.pdrobot.R;

import java.util.Calendar;
import java.util.concurrent.atomic.AtomicBoolean;


/**
 * Created by guiliu on 2016/7/11.
 */
public class Welcome extends BaseHomeActivity {
    private AtomicBoolean mConnected = new AtomicBoolean(false);
    private WifiManager mWifiManager;
    private IntentFilter mFilter;
    private ImageView logo;
    private AnimatorSet animationSet;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mWifiManager = (WifiManager)getSystemService(WIFI_SERVICE);
        openWifi();
        Log.e("weicome","year day month:"+getYear()+" "+getMonth()+" "+getDay());
        new Handler().postDelayed(new Runnable(){
            @Override
            public void run() {
                Intent intent = new Intent();

                if(!dateSet()) {
                    intent.putExtra("isConnected",mConnected.get());
                    intent.setClass(Welcome.this,DateTimeActivity.class);
                } else if(!wifiSet()) {
                    intent.setAction("android.intent.action.welcome");
                } else if(!mapSet()) {
                    intent.setClass(Welcome.this, MapUpLoadActivity.class);
                } else {
                    intent.setClass(Welcome.this, HomeActivity.class);
                }
                startActivity(intent);
                Welcome.this.finish();
            }
        }, 3000);
    }


    public boolean wifiSet() {
        return mConnected.get();
    }

    public static boolean mapSet() {
        String map_data = App.getInstance().getApplicationMapData();
        String altas_data = App.getInstance().getApplicationAtlasData();
        if(map_data != null && map_data.length() > 0 && altas_data != null && altas_data.length() > 0) {
            return true;
        }
        return false;
    }
    protected int getLayoutId() {
        return R.layout.welcome;
    }

    @Override
    protected void onResume() {
        super.onResume();
        mFilter = new IntentFilter();
        mFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
        mFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);

        logo = (ImageView)findViewById(R.id.logo);
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(logo,"alpha",0f,1f);
        ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(logo,"scalex",0f,1.0f);
        ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(logo,"scaley",0f, 1.0f);
//        ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(logo,"rotation", 0f, 360f);
        animationSet = new AnimatorSet();
        animationSet.playTogether(objectAnimator,objectAnimator1, objectAnimator2);
        animationSet.setDuration(3000);
        animationSet.start();
        registerReceiver(mReceiver, mFilter);
    }
    private BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            Log.e("weicome","NETWORK_STATE_CHANGED_ACTION");
            if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
                NetworkInfo info = (NetworkInfo) intent.getParcelableExtra(
                        WifiManager.EXTRA_NETWORK_INFO);

                mConnected.set(info.isConnected());
            }
        }
    };



    @Override
    protected void onDestroy() {
        super.onDestroy();
        animationSet.cancel();
        unregisterReceiver(mReceiver);
        Log.e("weicome","onDestroy");
    }

    /**
     *  打开WIFI
     */
    private void openWifi() {
        if (!mWifiManager.isWifiEnabled()) {
            mWifiManager.setWifiEnabled(true);
        }

    }


    public boolean dateSet() {

        if(getYear() == 2013 && getMonth() == 1 && getDay() == 1) {
            return false;
        }
        return true;
    }

    public int getYear() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.YEAR);
    }

    public int getMonth() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.MONTH) + 1;
    }

    public int getDay() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.DATE);
    }
}
